//
//  SEHospitalizeAPI.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/22.
//

import Moya

/// 就诊
enum SEHospitalizeAPI {
    case confirmOnlineReserve(doctorId: String, patientId: String, illnessDesc: String, hasAllergyHistory: Int, allergyHistory: String, pastHistory: String, imageIds: String)
    case confirmGuideDiagnosisInfo(illnessDesc: String, hasAllergyHistory: Int, allergyHistory: String, imageIds: String)
    
    case listOfInquiryMessages
    /// 导诊详情
    case detailOfGuideInquiry(id: String)
    /// 线上问诊详情
    case detailOfOnlineInquiry(id: String)
    
    /// 检查当前是否正在导诊
    case checkCurrentlyInGuideInquiry

    /// 我的问诊订单列表 - 个人中心
    case listOfHospitalizeOrder(source: Int = 2, pageNum: Int, pageSize: Int)

    /// 我的问诊订单详情 - 个人中心
    case detailOfHospitalizeOrder(id: String)
    
    /// 修改问诊状态
    /// 0-待支付，1支付成功后台发起，2医生同意，3患者收到医生同意，4医生结束发起，5患者同意,6医生拒绝,7患者拒绝结束,8患者未支付取消
    case changeInquiryStatus(id: String, status: String)

    /// 问诊提交评价
    case submitCommentOfHospitalizeOrder(objectId: String, objectType: Int = 2, detail: String = "", rank: Int, isRoot: Int = 1)
}

extension SEHospitalizeAPI: TargetType {
    var baseURL: URL { URL(string: SEApiManager.baseUrl)! }
    
    var path: String {
        switch self {
        case .confirmOnlineReserve: return "/smarteye/smarteye-inquiry/consultation/saveConsultation"
        case .confirmGuideDiagnosisInfo: return "/smarteye/smarteye-inquiry/guidance/saveGuidance"
        case .listOfInquiryMessages: return "/smarteye/smarteye-inquiry/unine/queryUnineDtoPage"
        case .detailOfGuideInquiry: return "/smarteye/smarteye-inquiry/guidance/getGuidanceDtoById"
        case .detailOfOnlineInquiry: return "/smarteye/smarteye-inquiry/consultation/getConsultationDtoById"
        case .checkCurrentlyInGuideInquiry: return "/smarteye/smarteye-inquiry/guidance/isGuidanceStart"
        case .listOfHospitalizeOrder: return "/smarteye/smarteye-inquiry/consultation/queryConsultationDtoList"
        case .detailOfHospitalizeOrder: return "/smarteye/smarteye-inquiry/consultation/getConsultationDtoById"
        case .changeInquiryStatus: return "/smarteye/smarteye-inquiry/consultation/updateConsultationStatus"
        case .submitCommentOfHospitalizeOrder: return "/smarteye/smarteye-inquiry/comment/createOrderComment"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .confirmOnlineReserve,
             .confirmGuideDiagnosisInfo,
             .listOfHospitalizeOrder,
             .changeInquiryStatus,
             .submitCommentOfHospitalizeOrder:
            return .post
        case .listOfInquiryMessages,
             .detailOfGuideInquiry,
             .detailOfOnlineInquiry,
             .checkCurrentlyInGuideInquiry,
             .detailOfHospitalizeOrder:
            return .get
        }
    }
    
    var sampleData: Data { "".data(using: .utf8)! }
    
    var task: Task {
        SELog("\(path) - \(parameters)")
        switch self {
        case .confirmOnlineReserve,
             .confirmGuideDiagnosisInfo,
             .listOfHospitalizeOrder,
             .changeInquiryStatus,
             .submitCommentOfHospitalizeOrder:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        case .listOfInquiryMessages,
             .checkCurrentlyInGuideInquiry:
            return .requestPlain
        case .detailOfGuideInquiry,
             .detailOfOnlineInquiry,
             .detailOfHospitalizeOrder:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        }
    }
    
    var headers: [String : String]? { SEApiManager.headers }
    
    private var parameters: [String: Any] {
        switch self {
        case let .confirmOnlineReserve(doctorId, patientId, illnessDesc, hasAllergyHistory, allergyHistory, pastHistory, imageIds):
            return ["doctorId": doctorId, "patientId": patientId, "illness": illnessDesc, "isAllergic": hasAllergyHistory, "allergicContent": allergyHistory, "pastHistory": pastHistory, "urlRelation": imageIds]
        case let .confirmGuideDiagnosisInfo(illnessDesc, hasAllergyHistory, allergyHistory, imageIds):
            return ["illness": illnessDesc, "isAllergic": hasAllergyHistory, "allergicContent": allergyHistory, "urlRelation": imageIds]
        case .listOfInquiryMessages,
             .checkCurrentlyInGuideInquiry:
            return [:]
        case .detailOfGuideInquiry(let id),
             .detailOfOnlineInquiry(let id):
            return ["id": id]
        case let .listOfHospitalizeOrder(source, pageNum, pageSize):
            return ["source": source, "pageNum": pageNum, "pageSize": pageSize]
        case let .detailOfHospitalizeOrder(id):
            return ["id": id]
        case let .changeInquiryStatus(id, status):
            return ["id": id, "dealStatus": status]
        case let .submitCommentOfHospitalizeOrder(objectId, objectType, detail, rank, isRoot):
            return ["objectId": objectId,
                    "objectType": objectType,
                    "detail": detail,
                    "rank": rank,
                    "isRoot": isRoot]
        }
    }
}
